package dao.Impl;

import dao.CompileFile;
import dao.CreateFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CompilePythonFile implements CompileFile {
    private String filename;
    public CompilePythonFile(String filename){
        this.filename=filename;
    }
    @Override
    public String compile() {
        return null;
    }

    @Override
    public String run() throws IOException {
        String line = "";
        String result = "";
        String[] cmd = new String[]{"D:\\Python37-32\\python.exe", "G:\\compile\\" + filename + ".py"};
        Process proc = Runtime.getRuntime().exec(cmd);
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
        try {
            proc.waitFor();
            while((line= stdInput.readLine())!=null){
                result+=line;
                result+="\n";

            }
            while ((line= stdError.readLine())!=null){
                result+=line;
                result+="\n";
            }

            System.out.println(result);
            return result;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return "false";
        }
    }

    public static void main(String[] args) {
        CreateFile createFile = new CreateFileImpl("count = 0\n" +
                "while (count < 9):\n" +
                "   print 'The count is:', count\n" +
                "   count = count + 1\n" +
                " \n" +
                "print \"Good bye!\"\n","py");
        createFile.setPythonFileName("test");
       createFile.writeFile();
        CompilePythonFile test = new CompilePythonFile("test");
        try {
            System.out.println(test.run());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
